package com.jkt.myself.csv;

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

// This method will not support parameterised getters of the pojo.
public class Reflector {
	@SuppressWarnings("unchecked")
	public static void main(String args[]) {
		try {
			Class objOfClassType = Class.forName("java.lang.String");
			
			String s1 = new String("Hello");
			String s2 = new String("World");
			
			List<String> pojoList = new ArrayList<String>();
			pojoList.add(s1);
			pojoList.add(s2);
			
			
			//Temporary populate the passedOnMethodsList with Dummy Method Names.Later it will be parameterized.
			List <String> passedOnMethodsList = new ArrayList<String>();
			passedOnMethodsList.add("hashCode");
			passedOnMethodsList.add("toString");

			//List<Method> MethodsInClassList = new ArrayList<Method>(Arrays.asList(objOfClassType.getDeclaredMethods()));
			List<Method> MethodsInClassList = new CopyOnWriteArrayList<Method>(Arrays.asList(objOfClassType.getDeclaredMethods())); 
			
			for (Method element : MethodsInClassList)
			{
				System.out.println(element.getName());
				if(!passedOnMethodsList.contains(element.getName()))
					MethodsInClassList.remove(element);
			}
			
			System.out.println("********************Methods to be invoked.********************");
			
			for(Method element : MethodsInClassList)
				System.out.println(element.getName());
				
				
			for(String pojo: pojoList)
			{
				StringBuilder sb = new StringBuilder();
				for(Method method : MethodsInClassList)
				{
					if(null !=method.invoke(pojo))
						sb.append(method.invoke(pojo));
					    sb.append(",");
				}	
				sb.append("\n");
				
				System.out.println(sb.toString());
				
			}
			
			// Verify method exists or remove it from the passed on methodList.  
//			String s =  m[0].invoke("", args).toString();
//			for (int i = 0; i < m.length; i++)
//				System.out.println(m[i].getReturnType());
				//System.out.println(m[i].toString());
		} catch (Throwable e) {
			System.err.println(e);
		}
	}
}